<template>
	<web-view :src="myurl"></web-view>
</template>

<script>
import { empty } from '../../../uni_modules/uv-ui-tools/libs/function/test'
import config from '@/api/config.js';
	export default {
		data() {
			return {
				myurl:"",
				formid:""
			}
		},
		onLoad:function(option){
			this.formid = option.formid
			this.myurl = this.url +'/preview.html?formid='+this.formid
			if(uni.getStorageSync('phone')){
				this.myurl = this.myurl + '&phone=' +uni.getStorageSync('phone')
			}
			else if(uni.getStorageSync('openId')){
				this.myurl = this.myurl + '&openid=' +uni.getStorageSync('openId')
			}
			uni.request({
				url:config.api() +'/QuestionnaireSettingController/GetQuestionnaireSetting',
				method:"GET",
				data:{
					formid:this.formid
				},
				success: (res) => {
					let openid = ''
					let phone = ''
					var type = 0
					console.log(res);
					res = res.data.data
					var num = res.ceiling
					if(!empty(res.phoneLimit)){
						this.login()
						if(uni.getStorageSync('phone')){
							phone = uni.getStorageSync('phone')
						}
						if(JSON.parse(res.phoneLimit).type == 1){
							type = 1
							num = JSON.parse(res.phoneLimit).ceiling
						}else{
							type = 0
							num = JSON.parse(res.phoneLimit).ceiling
						}
					}
					else if(!empty(res.weChatLimit)){
						this.login()
						if(uni.getStorageSync('openId')){
							openid = uni.getStorageSync('openId')
						}
						if(JSON.parse(res.weChatLimit).type == 1){
							type = 1
							num = JSON.parse(res.weChatLimit).ceiling
						}else{
							type=0
							num = JSON.parse(res.weChatLimit).ceiling
						}
					}
					uni.request({
						url:config.api() +'/UserAnswer/limitValidation',
						method:'POST',
						header:{
							"Content-Type": "application/x-www-form-urlencoded"
						},
						data:{
							formid:this.formid,
							ip: '',
							openid : openid ,
							phone: phone,
							type: type
						},
						success:(data)=>{
							console.log(data);
							data = data.data.data[0]
							console.log(openid,phone);
							if(openid && type == 1 && num !==0){
								if(data.openidNum >= num){
									uni.showModal({
										title:"系统提示",
										content:"你的答卷已上限",
										showCancel:false,
										success: (sure) => {
											if(sure.confirm){
												this.myurl = this.url + '/finally.html?formid='+this.formid
											}
										}
									})
								}
							}else{
								if(data.theDayAnswer >= num){
									uni.showModal({
										title:"系统提示",
										content:"你的答卷已上限",
										showCancel:false,
										success: (sure) => {
											if(sure.confirm){
												this.myurl = this.url + '/finally.html?formid='+this.formid
											}
										}
									})
								}
							}
							if(phone && type == 1 && num !==0){
								if(data.phoneNum >= num){
									uni.showModal({
										title:"系统提示",
										content:"你的答卷已上限",
										showCancel:false,
										success: (sure) => {
											if(sure.confirm){
												this.myurl = this.url + '/finally.html'+ 'finally.html?formid='+this.formid
											}
										}
									})
								}
							}else{
								if(data.theDayAnswer >= num){
									uni.showModal({
										title:"系统提示",
										content:"你的答卷已上限",
										showCancel:false,
										success: (sure) => {
											if(sure.confirm){
												this.myurl = this.url + '/finally.html'+ 'finally.html?formid='+this.formid
											}
										}
									})
								}
							}
						}
					})
				}
			})
		},
		methods: {
			login(){
				if(!uni.getStorageSync('token')||empty(uni.getStorageSync('token'))){
					uni.showToast({
						title:"请先登陆"
					})
					setTimeout(()=>{
						uni.navigateTo({
							url:"/pages/login/login?formid="+this.formid+'&why=true'
						})
					},1500)
				}
			}
		}
	}
</script>

<style>

</style>
